package com.bndg.blackjack

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AlertDialog

/**
 * @author r
 * @date 2024/10/12
 * @description BjGame 消息处理器
 */
object MsgManager {
    val playerId: String = ""
    var msgListener: InteractiveMsgListener? = null
    var myId: String = ""
    fun processMsg(context: Context, msgEntity: String) {
        val gameState =
            AppUtils.deserializeByJson(msgEntity, GameState::class.java)
        Log.d(
            ">>>>", "interactiveState:  ${gameState?.gameAction} ${gameState?.isPlaying} " +
                    "${gameState?.gameResult}"
        )
        gameState?.let {
            BlackjackGame.gameState = gameState
            if (gameState.isInviteMsg) {
                if (gameState.isPlayerAccepted) {
                    // 玩家接受邀请
                    msgListener?.receivedPlayerAcceptedMsg()
                } else {
                    // 是邀请消息
                    // todo 判断已经在Interactive的情况
                    Handler(Looper.getMainLooper()).post {
                        AlertDialog.Builder(context)
                            .setMessage("加入黑杰克?")
                            .setPositiveButton("确定") { _, _ ->
                                gameState.isPlayerAccepted = true
                                gameState.playerId = myId
                                val gameStateJson =
                                    AppUtils.serializeToJson(gameState)
                                msgListener?.sentMsg(gameStateJson)
                                msgListener?.receivedGameState(gameState)
//                                InteractiveActivity.start(context)
                            }
                            .setNegativeButton("取消") { _, _ ->
                                // 发送邀请
                                gameState.isPlayerAccepted = false
                                val gameStateJson =
                                    AppUtils.serializeToJson(gameState)
                                msgListener?.sentMsg(gameStateJson)
                            }
                            .show()
                    }
                }
            } else if (gameState.isDealerLeft || gameState.isPlayerLeft || !gameState.isPlayerAccepted) {
                // player离开 和 没有接受邀请
                if (!gameState.isPlayerAccepted) {
                    msgListener?.receivedRejectMsg()
                } else if (gameState.isPlayerLeft) {
                    msgListener?.receivedPlayerLeftMsg()
                } else if (gameState.isDealerLeft) {
                    msgListener?.receivedDealerLeftMsg()
                } else {

                }
            } else {
                msgListener?.receivedGameState(gameState)
            }
        }
    }
}

interface InteractiveMsgListener {
    fun receivedGameState(gameState: GameState)
    fun receivedPlayerLeftMsg()
    fun receivedRejectMsg()
    fun receivedDealerLeftMsg()
    fun receivedPlayerAcceptedMsg()
    fun sentMsg(gameStateJson: String)
}
